package com.lzy.web.admin;

import com.lzy.po.Blog;
import com.lzy.po.BlogQuery;
import com.lzy.po.User;
import com.lzy.service.BlogService;
import com.lzy.service.TagService;
import com.lzy.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * @autor shkstart
 * @create 2021-01-05 21:35)
 */

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @GetMapping("/blogs")
    public String blogsPage(@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC)Pageable pageable,
                            BlogQuery blogQuery,
                            Model model){
        model.addAttribute("page", blogService.listBlog(pageable,blogQuery));
        System.out.println(blogService.listBlog(pageable,blogQuery).getContent().toString());
        model.addAttribute("types", typeService.listType());
        return "admin/blogs";
    }

    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC)Pageable pageable,
                         BlogQuery blogQuery,
                            Model model){
        model.addAttribute("page", blogService.listBlog(pageable,blogQuery));
        return "admin/blogs :: blogList";
    }

    @GetMapping("/blogs/input")
    public String toInputPage(Model model){
        model.addAttribute("blog",new Blog());
        //初始化类型
        model.addAttribute("types", typeService.listType());
        //初始化标签
        model.addAttribute("tags",tagService.listTag());

        return "admin/blogs-input";
    }

    @PostMapping("/blogs")
    public String post(Blog blog, HttpSession session, RedirectAttributes attributes){
        System.out.println("==================="+blog.toString());
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));
        Blog b = null;
        if (blog.getId() != null){
            //修改
            b = blogService.updateBlog(blog.getId(),blog);
        }else {
            b = blogService.saveBlog(blog);
        }
        if(b == null){
            attributes.addFlashAttribute("message", "操作失败");
        }else{
            attributes.addFlashAttribute("message", "操作成功");
        }
        return "redirect:/admin/blogs";
    }

    @GetMapping("/blogs/{id}/input")
    public String toEditPage(@PathVariable("id") Long id, Model model){
        Blog blog = blogService.getBlog(id);
        blog.init();
        model.addAttribute("blog",blog);
        //初始化类型
        model.addAttribute("types", typeService.listType());
        //初始化标签
        model.addAttribute("tags",tagService.listTag());

        return "admin/blogs-input";
    }

    @GetMapping("/blogs/{id}/delete")
    public String deleteType(@PathVariable Long id,RedirectAttributes attributes){
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message","删除操作成功");
        return "redirect:/admin/blogs";  //重定位要加/
    }

}
